<?php

namespace Admin\Controller;
use Org\Util\Sync;

/**
 * 品类控制器
 * @author wangyuanlin
 * 2015-05-17
 */
class ClassifyController extends AdminController {

    /**
     * 品类列表
     * @author wangyuanlin
     * 2015-05-17
     */
    public function index() {
        $title = I('title');
        $map['status']  =   1;
        $map['title'] = array('like', '%' . (string) $title . '%');
        $list = $this->listsM('classify', $map,'ordnum asc');
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }

    /**
     * 新增品类
     * @author wangyuanlin
     * 2015-05-17
     */
    public function add($title = "", $intro = "", $gjz = "", $ordnum = "") {
        if (IS_POST) {
            // 上传文件 
            $setting = array(
                'mimes' => '', //允许上传的文件MiMe类型
                'maxSize' => 2 * 1024 * 1024, //上传的文件大小限制 (0-不做限制)
                'exts' => 'jpg,gif,png,jpeg', //允许上传的文件后缀
            );

            /* 调用文件上传组件上传文件 */
            /*             * 修改开始* */
            $pic_driver = C('PICTURE_UPLOAD_DRIVER');
            $upload = new \Think\Upload($setting, C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
            $info = $upload->upload();

            if (!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            } else {// 上传成功
                $data = array('title' => $title, 'intro' => $intro, 'gjz' => $gjz, 'fileurl' => $info["photo"]["url"], 'ordnum' => $ordnum, 'createdate' => NOW_TIME, 'lastupdate' => NOW_TIME);
                $id = M('classify')->add($data);
                if (!$id) {
                    $this->error("添加失败！");
                } else {
                    $this->success('添加成功！', U('index'));
                }
            }
        } else {
            $this->display();
        }
    }

    /**
     * 修改品类
     * @author wangyuanlin
     * 2015-05-17
     */
    public function edit($title = "", $intro = "", $ordnum = "") {
        $id = I('id');
        if (IS_POST) {
            
            // 上传文件 
            $setting = array(
                'mimes' => '', //允许上传的文件MiMe类型
                'maxSize' => 2 * 1024 * 1024, //上传的文件大小限制 (0-不做限制)
                'exts' => 'jpg,gif,png,jpeg', //允许上传的文件后缀
            );

            /* 调用文件上传组件上传文件 */
            /*             * 修改开始* */
            $pic_driver = C('PICTURE_UPLOAD_DRIVER');
            $upload = new \Think\Upload($setting, C('PICTURE_UPLOAD_DRIVER'), C("UPLOAD_{$pic_driver}_CONFIG"));
            $info = $upload->upload();
            
            $syncStatus = M('classify')->where(array('id' => $id))->getField('syncStatus');
            
            if($info){
                if($syncStatus == 0){
                    //已经上推过了
                    $data = array('title' => $title, 'intro' => $intro, 'fileurl' => $info["photo"]["url"], 'ordnum' => $ordnum, 'lastupdate' => NOW_TIME, 'syncStatus' => 2);
                }else{
                    //还没上推过了
                    $data = array('title' => $title, 'intro' => $intro, 'fileurl' => $info["photo"]["url"], 'ordnum' => $ordnum, 'lastupdate' => NOW_TIME);
                }
            }else{
                if($syncStatus == 0){
                    //已经上推过了
                    $data = array('title' => $title, 'intro' => $intro, 'ordnum' => $ordnum, 'lastupdate' => NOW_TIME, 'syncStatus' => 2);
                }else{
                    //还没上推过了
                    $data = array('title' => $title, 'intro' => $intro, 'ordnum' => $ordnum, 'lastupdate' => NOW_TIME);
                }
            }
            
            if (!M('classify')->where(array('id' => $id))->save($data)) {
                $this->error("修改失败！");
            } else {
                $this->success('修改成功！', U('index'));
            }
        } else {
            $data = M('classify')->where(array('id' => $id))->find();
            $this->data = $data;
            $this->display();
        }
    }
    
    /**
     * 删除品类
     * @author wangyuanlin
     * 2015-05-17
     */
    public function del($id = 0) {
        if ($id) {
            $classify = M("classify");
            
            $data = array('status'=>-1,'syncStatus'=>3);
            $result = $classify-> where(array('id'=>$id))->setField($data);
            
            if($result){
                $this->success('删除成功！', U('index'));
            }else{
                $this->error("删除失败！");
            }
        }
    }
    
    /**
     * 上推
     * @author wangyuanlin
     * 2015-06-23
     */
    public function push(){
        
        //获取要上推的数据源
        $list = M('Classify')->where(array('syncStatus'=>array('egt', 1)))->select();

        //执行上推操作
        $list = $this->pushParse('Classify',
            array('objectId'=>'id',
                'title'=>'title',
                'fileurl'=>'fileurl',
                'intro'=>'intro',
                'gjz'=>'gjz',
                'orderno'=>'ordnum',
                'status'=>'status',
                'syncStatus'=>'syncStatus'),
            function($src){
                //前调函数
                return $src;
            },
            function($src){
                //后调函数
                //数据上推到云端后，更新本地数据的同步状态 （syncStatus ＝ 0）
                M('Classify')->where('id='.$src['objectId'])->data(array('syncStatus'=>0))->save();
                return $src;
            },
            $list);
            
       $this->success('商品分类数据上推成功！', U('index'),true);
    }
}
